import axios from 'axios'
import { app } from '../main'
import { Notify } from 'vant'
// 创建axios实例

const service = axios.create({
  baseURL: 'http://8.138.102.145/api/', // api的base_url
  // baseURL: 'http://172.22.47.17:8090/order_system/',
  timeout: 20000 // 请求超时时间
})
service.interceptors.request.use(config => {
  config.headers.Authorization = window.sessionStorage.getItem('token')
  return config
}, (error) => {
  // 对请求错误做些什么
  return Promise.reject(error)
})
service.interceptors.response.use(config => {
  return config.data
}, error => {
  // 检查错误状态码
  if (error.response && error.response.status === 401) {
    // 清除令牌
    sessionStorage.removeItem('token');
    Notify({
      type: 'primary',
      message: "账号在别处登录"
    })
    // 跳转到登录页面
    app.$router.push('/login')
  }
  return Promise.reject(error);
})
axios.defaults.headers.common['Authorization'] = 'Bearer your-token';
axios.defaults.headers.common['Content-Type'] = 'application/json';

export default service
